package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.travel.dao.IWelfareConsumptionCouponDao;
import com.eling.elcms.travel.dao.IWelfareConsumptionCouponDetailDao;
import com.eling.elcms.travel.model.WelfareConsumptionCoupon;
import com.eling.elcms.travel.model.WelfareConsumptionCoupon.Status;
import com.eling.elcms.travel.model.WelfareConsumptionCouponDetail;
import com.eling.elcms.travel.model.WelfareConsumptionCouponDetail.OperateStatus;
import com.eling.elcms.travel.service.IWelfareConsumptionCouponDetailManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
public class WelfareConsumptionCouponDetailManagerImpl extends GenericManagerImpl<WelfareConsumptionCouponDetail, Long> implements IWelfareConsumptionCouponDetailManager{
	
	IWelfareConsumptionCouponDetailDao welfareDao;
	
	@Autowired
	IWelfareConsumptionCouponDao wccdao;
	
	@Autowired
	public void setWelfareDao(IWelfareConsumptionCouponDetailDao welfareDao) {
		this.dao = welfareDao;
		this.welfareDao = welfareDao;
	}
	
	@Autowired
	public void setWccdao(IWelfareConsumptionCouponDao wccdao) {
		this.wccdao = wccdao;
	}
	
	/**
	 * 取现操作
	 * @param cond 
	 * @author liyan
	 * 
	 */
	@Override
	public WelfareConsumptionCouponDetail saveEnchashment(
			WelfareConsumptionCouponDetail cond) {
		if(cond.getPrice()!=0){
			WelfareConsumptionCoupon wc = cond.getWelfareConsumptionCoupon();
			wc.setBalance(wc.getBalance()-cond.getPrice());
			wccdao.save(wc);
		}
		cond.setOperateStatus(OperateStatus.Cash);
		cond.setOperateDate(new Date());
		return save(cond);
	}

	/**
	 * 挂失
	 * @param wd
	 * @return
	 * @author liyan
	 */
	@Override
	public WelfareConsumptionCouponDetail saveLoss(
			WelfareConsumptionCouponDetail cond) {
		WelfareConsumptionCoupon wc = cond.getWelfareConsumptionCoupon();
		wc.setStatus(Status.Lose);
		wccdao.save(wc);
		cond.setOperateStatus(OperateStatus.Loss);
		cond.setOperateDate(new Date());
		return welfareDao.save(cond);
	}

	/**
	 * 退卡
	 * @param wd
	 * @return
	 * @author liyan
	 */
	@Override
	public WelfareConsumptionCouponDetail saveReturnCard(
			WelfareConsumptionCouponDetail cond) {
		WelfareConsumptionCoupon wc = cond.getWelfareConsumptionCoupon();
		wc.setBalance(Double.valueOf("0"));
		wc.setCardNo(null);
		wc.setStatus(Status.Returned);
		wccdao.save(wc);
		cond.setOperateStatus(OperateStatus.ReturnCard);
		cond.setOperateDate(new Date());
		return save(cond);
	}
}
